package tech.eyao.hs60x.converts;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.util.CollectionUtils;
import tech.eyao.hs60x.common.CommonConstants;
import tech.eyao.hs60x.dto.AccountDTO;
import tech.eyao.hs60x.dto.WatchDTO;
import tech.eyao.hs60x.model.domain.AccountDO;
import tech.eyao.hs60x.model.domain.AccountRelationDO;
import tech.eyao.hs60x.vo.AccountVO;

public class AccountConvert {

	public static AccountDO convert(AccountVO user) {
		AccountDO accountDO = new AccountDO();
		accountDO.setAccountType(user.getAccountType());
		accountDO.setDeviceId(user.getDeviceId());
		accountDO.setDeviceTag(user.getDeviceTag());
		accountDO.setHeadUrl(user.getHeadUrl());
		accountDO.setName(user.getName());
		accountDO.setNickName(user.getNickName());
		accountDO.setPassword(user.getPassword());
		accountDO.setPhone(user.getPhone());
		accountDO.setRegisterType(user.getRegisterType());
		accountDO.setSex(user.getSex());
		return accountDO;
	}

	/**
	 * 账户详情DTO
	 * 
	 * @param accountDO
	 * @param accountRelationDOs
	 * @param watchAccountList
	 * @return
	 */
	public static AccountDTO convertAccountDTO(AccountDO accountDO, List<AccountRelationDO> accountRelationDOs,
			List<AccountDO> watchAccountList) {
		
		if(null == accountDO) return null;
		// 构建DTO对象
		AccountDTO accountDTO = new AccountDTO();
		accountDTO.accountType = accountDO.getAccountType();
		accountDTO.nickName = accountDO.getNickName();
		accountDTO.accountId = accountDO.getId();

		if (!CollectionUtils.isEmpty(accountRelationDOs) && !CollectionUtils.isEmpty(watchAccountList)) {
			Map<Long, AccountDO> watchMap = watchAccountList.stream()
					.collect(Collectors.toMap(AccountDO::getId, AccountDO -> AccountDO));
			List<WatchDTO> watchDTOs = new ArrayList<>();

			accountRelationDOs.forEach((a) -> {
				// 获取设备账户详情
				WatchDTO watchDTO = convertWatchDto(watchMap.get(a.getRelationFunId()),
						null==a.getRoleCode() ? Boolean.FALSE : a.getRoleCode().equalsIgnoreCase(CommonConstants.ADMIN));
				if(null != watchDTO) {
					watchDTO.relationName = org.apache.commons.lang3.StringUtils.defaultString(a.getRelationName());
					watchDTOs.add(watchDTO);
				}
			});
			accountDTO.watch = watchDTOs;
			accountDTO.isBind = Boolean.TRUE;
		}
		return accountDTO;
	}

	/**
	 * 设备详情DTO
	 * 
	 * @param accountDO
	 * @return
	 */
	public static WatchDTO convertWatchDto(AccountDO accountDO, boolean isAdmin) {
		if(null == accountDO) return null;
		WatchDTO watchDTO = new WatchDTO();
		watchDTO.accountType = accountDO.getAccountType();
		watchDTO.birthday = accountDO.getBirthday()==null?0:accountDO.getBirthday().getTime();
		watchDTO.headUrl = accountDO.getHeadUrl();
		watchDTO.height = accountDO.getHeight();
		watchDTO.isAdmin = isAdmin;
		watchDTO.nickName = accountDO.getNickName();
		watchDTO.phone = accountDO.getPhone();
		watchDTO.deviceId = accountDO.getDeviceId();
		if(null != accountDO.getSex()) {
			watchDTO.sex = accountDO.getSex();
		}
		watchDTO.watchId = accountDO.getId();
		watchDTO.weight = accountDO.getWeight();
		return watchDTO;
	}

}
